#ifndef ADAPTIVEWIDGET_H
#define ADAPTIVEWIDGET_H

#include <type_traits>

#include <QGuiApplication>
#include <QWidget>
#include <QScreen>
#include <QShowEvent>
#include <QResizeEvent>

namespace DevEnvParam {

// 基准尺寸
constexpr double A_HEIGHT = 1044;
constexpr double A_WIDTH = 1920;
constexpr int CTRL_FIX_WIDTH = 80;
constexpr int CTRL_FIX_HEIGHT = 20;
}

template <class BaseWidget>
class AdaptiveWidget : public BaseWidget
{
    static_assert(std::is_base_of<QWidget, BaseWidget>::value, "BaseWidget must be a subclass of QWidget");
public:
    using BaseWidget::BaseWidget;
    using BaseWidget::resize;
    using BaseWidget::setFixedHeight;
    using BaseWidget::setFixedSize;
    using BaseWidget::setFixedWidth;
    using BaseWidget::setMaximumHeight;
    using BaseWidget::setMaximumSize;
    using BaseWidget::setMaximumWidth;
    using BaseWidget::setMinimumHeight;
    using BaseWidget::setMinimumSize;
    using BaseWidget::setMinimumWidth;

    explicit AdaptiveWidget(QWidget *parent = nullptr)
        : BaseWidget{parent}
    {
        init();
    }

    explicit AdaptiveWidget(const QString &title, QWidget *parent = nullptr)
        : BaseWidget{title, parent}
    {
        init();
    }
    double ratio() const
    {
        return (w_ratio + h_ratio) / 2;
    }

    void resize(const QSize &s)
    {
        resize(s.width(), s.height());
    }

    void resize(int w, int h)
    {
        BaseWidget::resize(w * w_ratio, h * h_ratio);
    }

    void setFixedHeight(int h)
    {
        BaseWidget::setFixedHeight(h * h_ratio);
    }

    void setFixedSize(const QSize &s)
    {
        setFixedSize(s.width(), s.height());
    }

    void setFixedSize(int w, int h)
    {
        BaseWidget::setFixedSize(w * w_ratio, h * h_ratio);
    }

    void setFixedWidth(int w)
    {
        BaseWidget::setFixedWidth(w * w_ratio);
    }

    void setMaximumHeight(int maxh)
    {
        BaseWidget::setMaximumHeight(maxh * h_ratio);
    }

    void setMaximumSize(const QSize &s)
    {
        setMaximumSize(s.width(), s.height());
    }

    void setMaximumSize(int maxw, int maxh)
    {
        BaseWidget::setMaximumSize(maxw * w_ratio, maxh * h_ratio);
    }

    void setMaximumWidth(int maxw)
    {
        BaseWidget::setMaximumWidth(maxw * w_ratio);
    }

    void setMinimumHeight(int minh)
    {
        BaseWidget::setMinimumHeight(minh * h_ratio);
    }

    void setMinimumSize(const QSize &s)
    {
        setMinimumSize(s.width(), s.height());
    }

    void setMinimumSize(int minw, int minh)
    {
        BaseWidget::setMinimumSize(minw * w_ratio, minh * h_ratio);
    }

    void setMinimumWidth(int minw)
    {
        BaseWidget::setMinimumWidth(minw * w_ratio);
    }

private:
    void init()
    {
        auto screen = QGuiApplication::primaryScreen();
        if (screen) {
            auto asize = screen->availableSize();
            w_ratio = asize.width() / DevEnvParam::A_WIDTH;
            h_ratio = asize.height() / DevEnvParam::A_HEIGHT;
        } else {
            w_ratio = 1.0;
            h_ratio = 1.0;
        }
    }
private:
    // 比例因子
    double w_ratio;
    double h_ratio;
};



#endif // ADAPTIVEWIDGET_H
